package com.example.a828502.sfdialtestdemo;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 828502 on 2016/5/16.
 */
public class MyListAdapter extends BaseAdapter {

    private List<ListItem> mList;
    private LayoutInflater layoutInflater;
    private Context context;

    public MyListAdapter(Context context,List<ListItem> data){
        this.context=context;
        this.mList=data;
        this.layoutInflater=LayoutInflater.from(context);
    }

    /**
     * 组件集合，对应list.xml中的控件
     * @author Administrator
     */
    public final class ViewContent{

        public TextView mTxtContactName,mTxtContactAddress,mTxtWaybillNo;
        public Button mBtnComplete,mBtnDialCall;
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }


    public ViewContent viewContent;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewContent=new ViewContent();
        if(convertView==null){
            viewContent=new ViewContent();
            //获得组件，实例化组件
            convertView=layoutInflater.inflate(R.layout.list_item, null);
            viewContent.mTxtContactName=(TextView)convertView.findViewById(R.id.textContactName);
            viewContent.mTxtContactAddress=(TextView)convertView.findViewById(R.id.textContactAddress);
            viewContent.mTxtWaybillNo = (TextView)convertView.findViewById(R.id.textWaybillNo);

            viewContent.mBtnComplete=(Button)convertView.findViewById(R.id.btnComplete);
            viewContent.mBtnComplete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onComBtnOnClickListener.OnComBtnOnClickListener(mList.get(position).getWaybillNo(),position,viewContent);
                }
            });
            viewContent.mBtnDialCall=(Button)convertView.findViewById(R.id.btnDialCall);
            viewContent.mBtnDialCall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDialBtnOnClickListener.OnDialBtnOnClickListener(mList.get(position).getWaybillNo(),position,viewContent);
 }
            });
            convertView.setTag(viewContent);
        }else{
            viewContent=(ViewContent)convertView.getTag();
        }
        //绑定数据
        final ListItem listItem = mList.get(position);
        viewContent.mTxtContactAddress.setText("地址:"+listItem.getCustomerAddress());
        viewContent.mTxtContactName.setText("姓名:"+listItem.getCustomerName());
        viewContent.mTxtWaybillNo.setText("运单号:"+listItem.getWaybillNo());
        return convertView;
    }


    //完成方法回调
    private OnComBtnOnClickListener onComBtnOnClickListener;
    public void setComBtnOnClickListener(OnComBtnOnClickListener onComClickListener) {
        this.onComBtnOnClickListener = onComClickListener;
    }

    public interface OnComBtnOnClickListener {
        void OnComBtnOnClickListener(String taskId, int position,ViewContent view);
    }


    //拨号方法回调

    private OnDialBtnOnClickListener onDialBtnOnClickListener;
    public void setDialBtnOnClickListener(OnDialBtnOnClickListener onDialClickListener) {
        this.onDialBtnOnClickListener = onDialClickListener;
    }

    public interface OnDialBtnOnClickListener {
        void OnDialBtnOnClickListener(String taskId, int position,ViewContent view);
    }


}
